package com.winning.sx.microframework.dao.sysmanage;

import com.winning.dapi.data.api.IDataAccessService;
import com.winning.dapi.data.entity.DataTable;
import com.winning.sx.microframework.model.exception.CustomException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

import java.util.Iterator;
import java.util.Map;
import java.util.Set;

@Repository
public class TableManageDao {

    @Autowired
    @Qualifier("dataSource")
    private IDataAccessService dataAccessService;


    public DataTable queryTableInfos() throws CustomException {
        DataTable table = null;
        try {
            String sql = "SELECT ROW_NUMBER() OVER (ORDER BY a.name) AS No,a.name,a.crdate,a.refdate,b.rows FROM sysobjects AS a INNER JOIN sysindexes AS b ON a.id = b.id  WHERE (a.type = 'u') AND (b.indid IN (0, 1))";
            table = dataAccessService.querySql(sql);
        } catch (Exception e) {
            throw new CustomException(-2001, "获取数据失败");
        }
        return table;
    }
    public DataTable queryTableColum(Map map) throws CustomException {
        DataTable table = null;
        try {
            String sql = "select COLUMN_NAME from information_schema.COLUMNS where table_name = :name";
            table = dataAccessService.querySqlUsingParam(sql,map);
        } catch (Exception e) {
            throw new CustomException(-2001, "获取数据失败");
        }
        return table;
    }

    public DataTable queryTableData(Map map) throws CustomException {
        DataTable table = null;
        try {
            String sql = "select * from " + map.get("table");
            table = dataAccessService.querySqlInPage(sql,Integer.parseInt(map.get("pageNum").toString()),Integer.parseInt(map.get("pageSize").toString()));
        } catch (Exception e) {
            throw new CustomException(-2001, "获取数据失败");
        }
        return table;
    }

    public int saveTableData(Map<String ,Object> map){
        String tableName = (String) map.get("tableName");
        String sql = "Insert into " + tableName + " (";
        map.remove("tableName");
        Set<String> strings = map.keySet();
        String columns = "";
        String values = "";
        for (String str : strings) {
            columns += "," + str;
            values += ",:" + str;
        }
        sql += columns.substring(1) + ") " + " Values (" + values.substring(1) + ")";
        int i = dataAccessService.executeSqlUsingParam(sql, map);
        return i;
    }
    public int updateTableData(Map<String ,Object> map){
        String tableName = (String) map.get("tableName");
        String sql = "Update " + tableName + " set ";
        map.remove("tableName");
        Set<String> strings = map.keySet();
        Iterator<String> iterator = strings.iterator();
        String idColumn = iterator.next();
        for (String str : strings) {
            if(!idColumn.equals(str))
                sql += str + "=:" + str + ",";
        }
        sql = sql.substring(0,sql.length()-1) + " where " + idColumn + "=:" + idColumn;
        int i = dataAccessService.executeSqlUsingParam(sql, map);
        return i;
    }

}
